<?php
/**
 * Created by PhpStorm.
 * User: stone
 * Date: 2018/9/21
 * Time: 下午2:31
 */

namespace App\Model\Traits;


use App\Model\Comment;
use App\Model\Favorite;
use App\Model\Zan;
use App\User;

trait Common
{

    public function user()
    {

        return $this->belongsTo(User::class);
    }


    //评论关联模型
    public function comment(){

        return $this->morphMany(Comment::class,'comment');
    }

    //关联收藏模型
    public function favorite(){

        return $this->morphMany(Favorite::class,'favorite');
    }

    //关联用户是否收藏
    public function isFavorite(User $user){
        //文章模型从收藏模型中去找到user_id 字段中是否存在
        return $this->favorite()->where('user_id',$user['id'])->first();
    }

    //关联zan模型
    public  function zan(){

        return $this->morphMany(Zan::class,'zan');
    }

    //是否点赞
    public function isZan(User $user){

        return $this->zan()->where('user_id',$user['id'])->first();
    }
}
